methmeth
Call a method on an object in an Array.prototype callback.
$ npm install --save methmeth
var meth = require('methmeth');
var friends = [
{
name: 'passy',
hobby: 'carrots',
getInfo: function () {
return this.name + ' likes ' + this.hobby;
}
},
{
name: 'sindre',
vehicle: 'unicorn taxi',
getInfo: function () {
return this.name + ' drives a ' + this.vehicle;
}
},
{
name: 'addy',
invented: 'google *',
getInfo: function () {
return this.name + ' created ' + this.invented;
}
}
];
Before
var myFriends = friends.map(function (item) {
return item.getInfo();
}).join('\n');
After
friends.map(meth('getInfo')).join('\n');
Pre-fill arguments
var friends = [
{
name: 'dave',
passion: 'dried mango',
getInfo: function (emotion) {
return this.name + ' loves ' + this.passion + emotion;
}
}
];
friends.map(meth('getInfo', '!!!!')).join('\n');
Related
- propprop - Pluck a property out of an object in an Array.prototype callback.